/* 
 * Project: colobok
 * and open the template in the editor.
 * @author: Sviatoslav Mysachenko <svyatoslavik@gmail.com>
 */


var basemodel = function(){
    function __save(modelName, data, _id){
        MapDataManager.saveModel({
            __model:modelName,
            _id: _id,
            data: $.toJSON(data)
        },function(resp){            
            _baseClass._id = resp._id;                        
            _baseClass.onSaved();
            $(_baseClass).trigger('saved');     
        });
        
    }
    function prepareBaseData(__baseclass){
        var key;
        for(key in __baseclass.__baseData){
            /*
            if(typeof __baseclass.__baseData[key]!= typeof __baseclass[key])
                throw('Type error: property ['+key+'] must have type "'+
                        typeof __baseclass.__baseData[key]+'" recived "'+
                        typeof __baseclass[key] +'"');
            */
            __baseclass.__baseData[key] = __baseclass[key];
        }
    }
    function initModelData(data, __baseclass){
        if(typeof data != 'object') return;
        var key;
        for(key in __baseclass.__baseData){
           /* if(typeof __baseclass.__baseData[key]!= typeof data[key])
                throw('INIT Type error: property ['+key+'] must have type "'+
                        typeof __baseclass.__baseData[key]+'" recived "'+
                        typeof data[key] +'"');*/
            __baseclass[key] = data[key];
        }
        if(typeof data._id=='string' && data._id )
            __baseclass._id = data._id;
    }
    var _baseClass= {
        _id:null,
        __baseData :{
            loca:1
        },
        __modelName:'',
        initModelData:function(data){
            initModelData(data, this);
        },
        save:function(){
            prepareBaseData(this);
            $(this).trigger('beforeSaved');
            __save(this.__modelName, this.__baseData, this._id);
        },
        beforeSaved:function(){},
        onSaved:function(){}
    }  
    return _baseClass;
}

var Advert = function(data){
    var cls = $.extend(new basemodel, {
        __modelName: 'Item',
        
        __baseData:{
            lat:0,
            lng:0,
            count:0
        },
        count:1,
        __marker:null,
        setMap:function(map){
            if(!this.__marker){
                 this.__marker = new google.maps.Marker({
                    //icon: Icons.getIcon(Icons.type.homesmile),
                    icon: Icons.getClusterIcon(Icons.type.base, this.count),
                    position: new google.maps.LatLng(this.lat, this.lng),
                    title:'Всего '+ this.count + ' обьектов',
                    map: map//,
//                    draggable:true
                 });
                 
//                 this.__marker.bindEvent('dragend', function(e){               
//                    this.loc = [e.latLng.lng(), e.latLng.lat()];
//                    this.save();
//                 }.bind(this));
            }
            else 
                this.__marker.setMap(map);
            return this;
        }
    });
    cls.initModelData(data);
    return cls;
}
    